package ar.com.tallerdos.smartdinner.sync;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.AsyncTask;
import android.util.Log;
import ar.com.tallerdos.smartdinner.cocina.MainActivity;
import ar.com.tallerdos.smartdinner.model.Product;

public class senderUpdateDB extends AsyncTask<Void, Void, Void> {
	private static final String COCINA_UPDATE = "modificarEstadoCocina.php";
	private Product product;
	
	public senderUpdateDB(MainActivity mainActivity) {
		// TODO Auto-generated constructor stub
	}

	@Override
	protected Void doInBackground(Void... params) {
		
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(Sincronizador.SERVER_URL + "/"  + COCINA_UPDATE);
			
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
			nameValuePairs.add(new BasicNameValuePair("id",product.getID().toString() ));
			nameValuePairs.add(new BasicNameValuePair("producto",product.getProductID().toString() ));
			nameValuePairs.add(new BasicNameValuePair("estado",product.getEstado().toString()));
			
			//Se agrega la fecha al pedido
			String fecha = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
			nameValuePairs.add(new BasicNameValuePair("fecha",fecha.replaceAll(" ", "%20")));
			
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			String response = httpclient.execute(httppost, responseHandler);
			
			String reverseString = response;
			Log.e("log_tag", "Conexion exitosa" + reverseString);
		} catch (Exception e) {
			Log.e("log_tag", "Error en conexion http" + e.toString());
		}
		return null;
	}

	public void setValues(Product prd){
		product = prd;
	}
	
}
